/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 钟广沛
 */
public class json2kml {

    static Gson  gson = new Gson();
    static KMLFormatter kml = new KMLFormatter();
    static File outFile ;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        String path ="";
        String path1 = "C:\\Users\\钟广沛\\Desktop\\out.kml";
        if(args.length != 2){
            System.out.print("get json path："+path);
            path = "C:\\Users\\钟广沛\\Desktop\\600205195";
            isBreakPointnew(100,279);
            return;
        }else{
            path = args[0];
            path1 = args[1];
            System.out.print("get json path："+path+"\n");
        }
        File file = new File(path);
       if(file.isFile() && file.exists()){ //判断文件是否存在
            
            File fileout = new File(path1);
            InputStreamReader read;
            OutputStreamWriter writer;
            int outnum = 0;
            try {
                read = new InputStreamReader(
                        new FileInputStream(file),"utf-8"); //考虑到编码格式
                
                writer = new OutputStreamWriter(
                        new FileOutputStream(fileout),"utf-8"); //考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                System.out.print("start gen kml file!!\n");
                writer.write(kml.getHeader());
                while((lineTxt = bufferedReader.readLine()) != null){
                    //System.out.println(lineTxt);
                    TrackerEntry entry =  preaseJson2Kml(lineTxt);
                    writer.write(kml.getOutput(entry));
                    outnum++;
                }
                writer.write(kml.getFooter());
                System.out.print("gen kml file count:"+outnum+"\n");
                System.out.print("gen kml file sucess!!\n");
                read.close();
                writer.flush();
                writer.close();
            } catch (Exception ex) {
                Logger.getLogger(json2kml.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
    }
    /**
	 * 判断拐点
	 */
	private static boolean isBreakPointnew(int lastbearing,int loc) {
            System.out.println("Enter isBreakPoint method... queue.size");

            int diffdir =  Math.abs(lastbearing-loc);
            if(diffdir > 180){

                    if(Math.min(lastbearing, loc) < 180 &&
                                    Math.max(lastbearing, loc) > 180)
                    {
                            diffdir = 360 - Math.max(lastbearing, loc) + Math.min(lastbearing, loc);
                    }else{
                            diffdir = 360-diffdir;
                    }
            }
            // 拐点判断
            boolean bpint = diffdir > 10;
            System.out.println("bearing ..." + diffdir + "> "
                            + 10 + " ," + bpint);
            return bpint;
    }
   
    class JsonLocation1{
        String spd;
        String dir;
        LL ll;
        Date ut;
        class LL{
            String latitude;
            String longitude;
            public String toString(){
                 return "latitude:"+latitude+" longitude:"+longitude;
            }
        }
        class Date{
            String $date;
        }
        public String toString(){
            return "spd:"+spd+" dir:"+dir+" ll:"+ll.toString();
        }
    }

    public static TrackerEntry  preaseJson2Kml(String line){
        //String testString="{\"spd\":\"100\",\"dir\":20,\"ll\":{\"latitude\":22.56366,\"longitude\":114.029796},\"cll\":21}";
        JsonLocation1 loc = gson.fromJson(line, JsonLocation1.class);
        //System.out.println(loc.toString());
        Location location = new Location("gps");
        location.reset();
        location.setSpeed(Float.valueOf(loc.spd));
        location.setBearing(Float.valueOf(loc.dir));
        location.setLatitude(Double.valueOf(loc.ll.latitude));
        location.setLongitude(Double.valueOf(loc.ll.longitude));
        TrackerEntry entry = TrackerEntry.createEntry(location,0);
        entry.setTimestamp(loc.ut.$date);
        return entry;
    }
}
